#encoding:utf8

import math
from django.shortcuts import render_to_response, get_object_or_404, HttpResponseRedirect
from django.core.urlresolvers import reverse

# Create your views here.
# from models import Tasker
# from utils.pages import wrap_pages
# from utils.tools import get_request_field

from utils.decorators import wrap_exception, wrap_view_api

from config import AUTH_CODE
from controler import tasker_pool
from logger import logger


@wrap_exception
@wrap_view_api(logger=logger, auth=None)
def start_task(request, id):
    id = int(id)
    tasker_pool.start_task(id)
    return 0


@wrap_exception
@wrap_view_api(logger=logger, auth=None)
def shutdown_task(request, id):
    id = int(id)
    tasker_pool.shutdown_task(id)
    return 0


import flag


@wrap_view_api(logger=logger, auth=None)
def run(request):
    # 启动后台的任务监控scheduler-control开始任务
    from controler import run
    logger.info("start monitor ... ")
    if flag.RUN_CONTROLER == 0:
        run()
        flag.RUN_CONTROLER = 1
    else:
        return "already started"
